﻿using FRMS.BUS.ServerPlatform;
using FRMS.DAL;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace FRMS.BUS
{
    public class DeviceBUS
    {
        public ResponseMessage AddDevice(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceDAL da = new DeviceDAL();

            int ip_count = da.CheckDeviceIp(deviceInfo.DeviceIP, deviceInfo.projectID);

            if (ip_count > 0)
            {
                message.errcode = 1;
                message.errmsg = "设备IP已存在，新增失败";
            }
            else
            {
                int no_count = da.CheckDeviceNO(deviceInfo.DeviceNO, deviceInfo.ID);
                if (no_count > 0 && !string.IsNullOrEmpty(deviceInfo.DeviceNO))
                {
                    message.errcode = 1;
                    message.errmsg = "设备编号已存在，新增失败";
                }
                else
                {
                    bool result = da.AddDevice(deviceInfo);
                    if (result)
                    {
                        message.errcode = 0;
                    }
                    else
                    {
                        message.errcode = 1;
                        message.errmsg = "新增设备失败";
                    }
                }

            }

            return message;
        }

        public ResponseMessage EditDevice(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceDAL da = new DeviceDAL();
            if (!string.IsNullOrEmpty(deviceInfo.DeviceNO))
            {
                int no_count = da.CheckDeviceNO(deviceInfo.DeviceNO, deviceInfo.ID);
                if (no_count > 0)
                {
                    message.errcode = 1;
                    message.errmsg = "设备编号已存在，修改失败";
                }
                else
                {
                    bool result = da.UpdateDevice(deviceInfo);
                    if (result)
                    {
                        message.errcode = 0;
                    }
                    else
                    {
                        message.errcode = 1;
                        message.errmsg = "修改设备信息失败";
                    }
                }
            }
            else
            {
                bool result = da.UpdateDevice(deviceInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改设备信息失败";
                }
            }


            return message;
        }

        public ResponseMessage DeleteDevice(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceDAL da = new DeviceDAL();
            if (da.CheckVoucher(deviceInfo))
            {
                bool result = da.DelDevice(deviceInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "删除设备失败";
                }
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "该设备下有凭证信息，请先删除凭证";
            }


            return message;
        }

        public ResponseMessage QueryDevice(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceDAL da = new DeviceDAL();
            message.errcode = 0;
            message.data = da.QueryDeviceInfo(deviceInfo);
            message.total = da.QueryDeviceInfoCount(deviceInfo);

            return message;
        }

        public ResponseMessage QueryDeviceList(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceDAL da = new DeviceDAL();
            message.errcode = 0;
            message.data = da.QueryDeviceList(deviceInfo);
            message.total = da.QueryDeviceListCount(deviceInfo);

            return message;
        }

        public ResponseMessage QueryDevType()
        {
            ResponseMessage message = new ResponseMessage();
            DeviceDAL da = new DeviceDAL();
            message.errcode = 0;
            message.data = da.QueryDevType();

            return message;
        }

        public ResponseMessage LoadDevice(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceDAL da = new DeviceDAL();
            message.errcode = 0;
            message.data = da.LoadDeviceInfo(deviceInfo.ID);

            return message;
        }

        public ResponseMessage QueryTKDevice()
        {
            ResponseMessage message = new ResponseMessage();

            DeviceInfo deviceInfo = new DeviceInfo();
            DeviceDAL da = new DeviceDAL();
            deviceInfo.DeviceType = 1;
            message.data = da.QueryDeviceInfo(deviceInfo);
            message.total = da.QueryDeviceInfoCount(deviceInfo);
            message.errcode = 0;

            return message;
        }

        public ResponseMessage ImportDevice(DeviceInfo deviceInfo)
        {
            ResponseMessage message = new ResponseMessage();
            DeviceDAL da = new DeviceDAL();
            bool result = da.ImportDevice(deviceInfo);

            if (result)
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "导入业主信息失败";
            }

            return message;
        }

    }
}
